<template>
<el-card style="width: 500px;">
  <el-form label-width="60px" size="small">
    <el-upload
      class="avatar-uploader"
      action="http://localhost:8082/file/upload"
      :show-file-list="false"
      :on-success="handleAvatarSuccess">
      <img v-if="form.photo" :src="form.photo" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-form-item label="用户名">
      <el-input v-model="form.username" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="昵称">
      <el-input v-model="form.nickname" autocomplete="off"></el-input>
    </el-form-item>
<!--    <el-form-item label="密码" v-show="showPwd" v-if="showPwd==true">-->
<!--      <el-input v-model="form.password" autocomplete="off"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="照片">
      <el-input v-model="form.photo" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="年龄">
      <el-input v-model="form.age" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="性别">
      <el-input v-model="form.sex" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="电话">
      <el-input v-model="form.phone" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="地址">
      <el-input v-model="form.address" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model="form.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</el-card>
</template>

<script>
export default {
  name: "Person",
  data() {
    return {
      form: {},
      user: {}
    }
  },
  created() {
    this.load()
  },
  methods:{
    async load(){
      let username = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")).username : ""
      if (username) {
        this.request.post("/user/findUserByName?username=" + username).then(res => {
          if (res) {
            this.form = res
            console.log(res.password)
          }
        })
      }
    },
    save(){
      this.request.post("/user/editUser", this.form).then(res => {
        if (res){
          this.$message.success("保存成功")
          this.load()
          this.$emit('refreshUser')
        }else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAvatarSuccess(res) {
      this.form.photo = res
    }
  }
}
</script>

<style scoped>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
  border-radius: 30%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 158px;
  height: 158px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
